package com.example.base.view

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
//基础Activity
abstract class BaseActivity<VDB:ViewDataBinding>:AppCompatActivity() {
    protected lateinit var mDataBinding: VDB

    /**
     * 参数savedInstanceState,用于获取Activity的实例状态
     * 场景:Activity重新创建(屏幕旋转,配置更新)
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //使用DataBinding设置布局
        mDataBinding = DataBindingUtil.setContentView(this, getLayoutId())
        //初始化UI
        initView()
    }

    /**
     * 获取Activity的实例状态
     */
    override fun onRestoreInstanceState(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)
        savedInstanceState?.run {
            getActivityState(this)
        }
    }

    /**
     * 保存当前Activity的状态
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        saveActivityState(outState)
    }

    protected abstract fun getLayoutId(): Int

    protected open fun initView(){

    }

    protected fun getActivityState(state: Bundle) {

    }
    protected fun saveActivityState(outState:Bundle){

    }

}